//请实现两个函数，分别用来序列化和反序列化二叉树。 
//
// 你需要设计一个算法来实现二叉树的序列化与反序列化。这里不限定你的序列 / 反序列化算法执行逻辑，你只需要保证一个二叉树可以被序列化为一个字符串并且将这个字
//符串反序列化为原始的树结构。 
//
// 提示：输入输出格式与 LeetCode 目前使用的方式一致，详情请参阅 LeetCode 序列化二叉树的格式。你并非必须采取这种方式，你也可以采用其他的方
//法解决这个问题。 
//
// 
//
// 示例： 
// 
// 
//输入：root = [1,2,3,null,null,4,5]
//输出：[1,2,3,null,null,4,5]
// 
//
// 
//
// 注意：本题与主站 297 题相同：https://leetcode-cn.com/problems/serialize-and-deserialize-
//binary-tree/ 
//
// Related Topics 树 深度优先搜索 广度优先搜索 设计 字符串 二叉树 👍 410 👎 0


package leetcode.editor.cn;

import utils.CreateNode;
import utils.TreeNode;

import java.util.*;

class 序列化二叉树 {
    public static void main(String[] args) {
        Codec codec = new 序列化二叉树().new Codec();
        CreateNode createNode = new CreateNode();
        Queue<Integer> tree = new LinkedList<>(Arrays.asList(1,2,3,null,null,4,5));
        TreeNode root = new TreeNode(tree.remove());
        createNode.createTree(tree, root);
        System.out.println(root.printTree());
        String s = codec.serialize(root);
        System.out.println(s);
        System.out.println(codec.deserialize(s).printTree());
    }

//leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode(int x) { val = x; }
     * }
     */
    public class Codec {

        // Encodes a tree to a single string.
        public String serialize(TreeNode root) {

            // 利用队列进行层序遍历
            if (root == null) {
                return "[]";
            }
            StringBuilder tree = new StringBuilder("[");

            Queue<TreeNode> queue = new LinkedList<>();
            queue.offer(root);

            while (!queue.isEmpty()) {
                int size = queue.size();

                for (int i = 0; i < size; i++) {
                    TreeNode node = queue.poll();
                    if (node == null) tree.append("null,");
                    else {
                        tree.append(node.val + ",");
                        queue.offer(node.left);
                        queue.offer(node.right);
                    }
                }

            }
            tree.deleteCharAt(tree.length() - 1);
            tree.append("]");
            return tree.toString();
        }

        // Decodes your encoded data to tree.
        public TreeNode deserialize(String data) {
            if (data.equals("[]")) return null;
            String[] vals = data.substring(1, data.length() - 1).split(",");
//            System.out.println(Arrays.toString(vals));

            TreeNode root = new TreeNode(Integer.parseInt(vals[0]));
            Queue<TreeNode> tree = new LinkedList<TreeNode>() {{
                add(root);
            }};
            int i = 1;
            while (!tree.isEmpty()) {
                TreeNode node = tree.remove();
                if (!vals[i].equals("null")) {
                    TreeNode left = new TreeNode(Integer.parseInt(vals[i]));
                    node.left = left;
                    tree.offer(left);
                }
                i++;
                if (!vals[i].equals("null")) {
                    TreeNode right = new TreeNode(Integer.parseInt(vals[i]));
                    node.right = right;
                    tree.offer(right);
                }
                i++;
            }
            return root;
        }
    }

// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.deserialize(codec.serialize(root));
//leetcode submit region end(Prohibit modification and deletion)

}
